package com.tanhua.dubbo.api;

import com.tanhua.domain.db.AnalysisByDay;
import com.tanhua.domain.vo.AnalysisSummaryVo;
import com.tanhua.dubbo.api.db.AnalysisByDayApi;
import com.tanhua.dubbo.mapper.AnalysisByDayMapper;
import com.tanhua.dubbo.utils.ComputeUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class AnalysisByDayApiImpl implements AnalysisByDayApi {

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Override
    public AnalysisSummaryVo summary(String todayStr) {
        String yesTodayStr = new DateTime(todayStr).minusDays(1).toString("yyyy-MM-dd");
        AnalysisSummaryVo summaryVo = new AnalysisSummaryVo();
        //        private Long cumulativeUsers;
        // 累计用户数
        Long cumulativeUsers = analysisByDayMapper.queryCumulativeUsers();
        summaryVo.setCumulativeUsers(cumulativeUsers);
        String pass7Day = new DateTime(todayStr).minusDays(7).toString("yyyy-MM-dd");
        Long activePassWeek = analysisByDayMapper.queryPassDay(pass7Day);
        //        private Long activePassWeek;//过去7天活跃用户
        summaryVo.setActivePassWeek(activePassWeek);

        String pass30Day = new DateTime(todayStr).minusDays(30).toString("yyyy-MM-dd");
        Long activePassMonth = analysisByDayMapper.queryPassDay(pass30Day);
//        private Long activePassMonth;//过去30天活跃用户数
        summaryVo.setActivePassMonth(activePassMonth);


//        private Long newUsersToday; //今日新增用户数量
        Long newUsersToday = analysisByDayMapper.qureyNumByColumAndDate("num_registered",todayStr);
//        private Long newUsersYesToday; //昨日新增用户数量
        Long newUsersYesToday = analysisByDayMapper.qureyNumByColumAndDate("num_registered",yesTodayStr);
        /**
         * 今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
         *      BigDecimal : 商业数字格式
         *
         */
//        private BigDecimal newUsersTodayRate;
        BigDecimal newUsersTodayRate = ComputeUtil.computeRate(newUsersToday.intValue(),newUsersYesToday.intValue());
        summaryVo.setNewUsersToday(newUsersToday);
        summaryVo.setNewUsersTodayRate(newUsersTodayRate);



//        private Long loginTimesToday; //今日登录次数
        Long loginTimesToday = analysisByDayMapper.qureyNumByColumAndDate("num_login",todayStr);
//        private Long loginTimesYesToday; //昨日登录次数
        Long loginTimesYesToday = analysisByDayMapper.qureyNumByColumAndDate("num_login",yesTodayStr);
//        private BigDecimal loginTimesTodayRate;// 今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday.intValue(),loginTimesYesToday.intValue());
        summaryVo.setLoginTimesToday(loginTimesToday);
        summaryVo.setLoginTimesTodayRate(loginTimesTodayRate);

//        private Long activeUsersToday;//今日活跃用户数量
        Long activeUsersToday = analysisByDayMapper.qureyNumByColumAndDate("num_active",todayStr);
//        private Long activeUsersYesToday;//昨日活跃用户数量
        Long activeUsersYesToday = analysisByDayMapper.qureyNumByColumAndDate("num_active",yesTodayStr);
//        private BigDecimal activeUsersTodayRate;//今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday.intValue(),activeUsersYesToday.intValue());
        summaryVo.setActiveUsersToday(activeUsersToday);
        summaryVo.setActiveUsersTodayRate(activeUsersTodayRate);

        return summaryVo;
    }

    @Override
    public void insert(AnalysisByDay analysisByDay) {
        analysisByDayMapper.insert(analysisByDay);
    }
}

